package cn.yunyichina.provider.pay.alipay.service.impl;

import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.pay.alipay.config.Alipay;
import cn.yunyichina.provider.pay.alipay.constant.ResponseEnum;
import cn.yunyichina.provider.pay.alipay.entity.common.RefundRequest;
import cn.yunyichina.provider.pay.alipay.service.AlipayRefundService;
import cn.yunyichina.provider.pay.alipay.service.BaseRefundService;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * Created by Lullaby on 2016/11/24.
 */
@Service
public class AlipayRefundServiceImpl extends BaseRefundService implements AlipayRefundService {

    private static final Logger logger = LoggerFactory.getLogger(AlipayRefundServiceImpl.class);

    public ResponseObject refundAlipay(String data) {
        logger.info("AlipayRefundServiceImpl.refundAlipay.data -> {}", data);

        ResponseObject response = new ResponseObject();

        RefundRequest refundRequest = JsonUtils.parseObject(data, RefundRequest.class);

        AlipayClient alipayClient = new DefaultAlipayClient(
                this.getConfig(Alipay.ALIPAY_GATEWAY_OPENAPI),
                refundRequest.getAppId(),
                refundRequest.getPrivateKey(),
                refundRequest.getFormat(),
                refundRequest.getCharset(),
                refundRequest.getPublicKey(),
                refundRequest.getSignType()
        );

        AlipayTradeRefundRequest alipayTradeRefundRequest = new AlipayTradeRefundRequest();
        alipayTradeRefundRequest.setBizContent(refundRequest.getBizContent());

        try {
            logger.info("AlipayRefundServiceImpl.refundAlipay.alipayTradeRefundRequest -> {}", JsonUtils.toJsonString(alipayTradeRefundRequest));
            AlipayTradeRefundResponse alipayTradeRefundResponse = alipayClient.execute(alipayTradeRefundRequest);
            logger.info("AlipayRefundServiceImpl.refundAlipay.alipayTradeRefundResponse -> {}", JsonUtils.toJsonString(alipayTradeRefundResponse));

            if (alipayTradeRefundResponse.isSuccess()) {
                response.setResultObject(buildAlipayRefundResponse(alipayTradeRefundResponse));
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.ALIPAY_REFUND_FAILURE.getCode(),
                        ResponseEnum.ALIPAY_REFUND_FAILURE.getMessage(),
                        this.getTradeFailureResponse(alipayTradeRefundResponse)
                );
            }
        } catch (AlipayApiException e) {
            response.setResultCodeAndMessage(
                    ResponseEnum.ALIPAY_REFUND_EXCEPTION.getCode(),
                    ResponseEnum.ALIPAY_REFUND_EXCEPTION.getMessage()
            );
            logger.error("AlipayRefundServiceImpl.refundAlipay.AlipayApiException", e);
        }

        return response;
    }

}
